package com.topscore.uke.ui.fragments;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.NewsContent;
import com.topscore.uke.mvp.contractors.GetActivitiesContractor;
import com.topscore.uke.mvp.presenters.GetActivitiesPresenter;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.activities.NewsDetailActivity;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.ui.widgets.RecyclerAdapterLoadMoreView;
import com.topscore.uke.utils.DisplayImageOptionsManager;
import com.topscore.uke.utils.SnackBarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivitiesPageFragment extends BaseSupportFragment implements GetActivitiesContractor.View {

    @Bind(R.id.tool_bar)
    Toolbar toolBar;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;

    private BaseQuickAdapter<NewsContent, BaseViewHolder> mRecyclerAdapter;

    private GetActivitiesContractor.Presenter mPresenter;

    private LoadService mLoadService;

    private View rootView;

    public ActivitiesPageFragment() {
        // Required empty public constructor

        mPresenter = new GetActivitiesPresenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_activities_page, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        initToolBar();
        mLoadService = LoadSir.getDefault().register(recyclerView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mPresenter.getActivitiesData();
            }
        });
        mRecyclerAdapter = new BaseQuickAdapter<NewsContent, BaseViewHolder>(R.layout.activities_list_item, new ArrayList()) {
            @Override
            protected void convert(BaseViewHolder holder, NewsContent item) {
                ImageView imageView = holder.getView(R.id.image_iv);
                ImageLoader.getInstance().displayImage(item.getTitleImage(),
                        imageView,
                        DisplayImageOptionsManager.getInstance().getDefaultOptions());
                holder.setText(R.id.title_tv, item.getTitle());
                Date date = new Date(item.getIssueTime());
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
                holder.setText(R.id.time_tv, dateFormat.format(date));
                holder.itemView.setOnClickListener(view -> {
                    Intent intent = new Intent(mContext, NewsDetailActivity.class);
                    intent.putExtra("title_code",item.getTitleCode());
                    startActivity(intent);
                });
            }
        };
        mRecyclerAdapter.setEnableLoadMore(true);
        mRecyclerAdapter.setLoadMoreView(new RecyclerAdapterLoadMoreView());
        mRecyclerAdapter.setOnLoadMoreListener(() -> mPresenter.loadNextPage(), recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(mRecyclerAdapter);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        swipeRefresh.setOnRefreshListener(()->initData());
        initData();
    }

    private void initData() {
        mPresenter.getActivitiesData();
//        List list = new ArrayList();
//        for (int i = 0; i < 12; i++) {
//            list.add(new Object());
//        }
//        mRecyclerAdapter.setNewData(list);
    }

    private void initToolBar() {
        setHasOptionsMenu(true);
        toolBar.setTitle("");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolBar);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
        swipeRefresh.setRefreshing(false);
    }

    @Override
    public void onFail(int code, String errorMessage) {
        SnackBarUtil.getInstance(rootView).showShort(errorMessage).show();
        mLoadService.showCallback(ErrorCallBack.class);
    }

    @Override
    public BaseQuickAdapter getListAdapter() {
        return mRecyclerAdapter;
    }
}
